package com.itheima.reggie.web;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.R;
import com.itheima.reggie.entity.Employee;
import com.itheima.reggie.service.IEmployeeService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;


import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;


@Slf4j
@RestController
@RequestMapping("/employee")
public class EmployeeController {
    @Autowired
    IEmployeeService iEmployeeService;
    /**
     * 员工登录
     * @param request
     * @param employee
     * @return
     */
    @PostMapping("/login")
    public R<Employee> login(@RequestBody Employee employee, HttpServletRequest request){
        //1.将页面提交的密码password进行md5加密处理, 得到加密后的字符串
        String username = employee.getUsername();
        String psw = employee.getPassword();
        String password = DigestUtils.md5DigestAsHex(psw.getBytes());
        //2.根据页面提交的用户名username查询数据库中员工数据信息
        LambdaQueryWrapper<Employee> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Employee::getUsername,username);
        Employee employee1 = iEmployeeService.getOne(queryWrapper);

        //3.如果没有查询到, 则返回登录失败结果
        if(employee1 == null){
            return R.error("登陆失败");
        }
        //4.密码比对，如果不一致, 则返回登录失败结果
        if (!employee1.getPassword().equals(password)){
            return R.error("密码错误登录失败");
        }
        //5.查看员工状态，如果为已禁用状态，则返回员工已禁用结果
        if (employee1.getStatus() == 0){
            return R.error("登陆失败账号已禁用");
        }
        //6.登录成功，将员工id存入Session, 并返回登录成功结果
        request.getSession().setAttribute("employee",employee1.getId());


        return R.success(employee1);
    }

    /**
     * 员工退出
     * @param session
     * @return
     */
    @PostMapping("/logout")
    public R<String>logout(HttpSession session){
        //清理Session中保存的当前登录员工的id
        session.removeAttribute("employee");
        return R.success("退出成功");
    }

    @PostMapping
    public R<String>save(@RequestBody Employee employee,HttpServletRequest request){
        log.info("新增员工,员工信息:{}",employee.toString());
        //设置初始密码123456,需要进行md5加密处理
        employee.setPassword(DigestUtils.md5DigestAsHex("123456".getBytes()));
        //employee.setCreateTime(LocalDateTime.now());
        //employee.setUpdateTime(LocalDateTime.now());
        //获得当前登录用户的id
        //Long empId = (Long) request.getSession().getAttribute("employee");
        //employee.setCreateUser(empId);
       // employee.setUpdateUser(empId);

        iEmployeeService.save(employee);
        return R.success("新增员工成功");
    }

    /**
     * 分页查询员工信息
     * @param page
     * @param pageSize
     * @param name
     * @return
     */
    @GetMapping("/page")
    public R<Page> pageEmp(String page,String pageSize,String name){
        log.info("page employee:{},{},{}",page,pageSize,name);
        //1.构建条件构建器和分页对象
        //limit(page-1)*pageSize,pageSize
        Page<Employee> pageConditon = new Page<>(Integer.parseInt(page), Integer.parseInt(pageSize));
        //构造条件构造器
        LambdaQueryWrapper<Employee> wrapper = new LambdaQueryWrapper<>();
        //添加过滤条件
        //模糊查询时生效 : name like %name数值%
        wrapper.like(name != null && name != "",Employee::getName,name);
        //添加排序条件
        wrapper.orderByDesc(Employee::getUpdateTime);
        //2.查询
        iEmployeeService.page(pageConditon,wrapper);
        return R.success(pageConditon);
    }

    /**
     * 更新员工信息和修改员工的状态,使用的同一个api
     * @param employee
     * @param session
     * @return
     */
    @PutMapping
    public R<String> update(@RequestBody Employee employee,HttpSession session){
        log.info("updateStatus employee:{}",employee);
        //1.进一步封装需要更新的employee实体对象
        //获取用户信息
        //Long empId = (Long) session.getAttribute("employee");
        long id = Thread.currentThread().getId();
        log.info("线程id为:{}",id);
        //更新用户状态,获取员工Id
        //设置更新用户的用户Id
        //employee.setUpdateUser(empId);
        //设置更新时间
       // employee.setUpdateTime(LocalDateTime.now());
        //2.更新员工信息
        iEmployeeService.updateById(employee);
        return  R.success("员工信息修改成功");
    }

    /**
     * 编辑员工信息---先查询后更新(更新和修改状态使用同一个接口)
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public R<Employee> queryOneEmp(@PathVariable Long id){
        log.info("queryOneEmp employee:{}",id);
        //1.查询员工信息
        Employee emp = iEmployeeService.getById(id);
        if (emp == null){
            return R.error("没有查询到对应的员工信息");
        }
        return R.success(emp);
    }




}
